<?php
	session_start();
	include("load-settings.php");

	if(!isset($_SESSION['user']))
		header("Location: login.php");

	$user = $_SESSION['user'];

	$result = mysql_query("SELECT * FROM user WHERE id = $user");
	$row = mysql_fetch_array($result);
	$max_claims = $row['max_claims'];
	$claim_delay = $row['claim_time_limit'];
	$type = $row['type'];

	if($row['type'] == 0)
		header("Location: home.php");

	if(isset($_GET['id']))
	{
		$order = mysql_real_escape_string($_GET['id']);

		$result = mysql_query("SELECT * FROM order_record WHERE id = $order");

		if(mysql_num_rows($result) == 0)
		{
			header("Location: booster.php");
		}

		$row = mysql_fetch_array($result);

		$claimed_result = mysql_query("SELECT * FROM booster_order WHERE active <> 0 AND booster = $user");
		$last_claim_result = mysql_query("SELECT * FROM booster_order WHERE booster = $user ORDER BY timestamp DESC LIMIT 1");
		$last_claim_row = mysql_fetch_array($last_claim_result);
		$last_claim = $last_claim_row['timestamp'];

		if(mysql_num_rows($claimed_result) >= $max_claims && $type != 2)
		{
			echo "<script>window.onload = function () { alert('You have reached the maximum number of concurrent claims.'); window.location = 'booster.php'; }</script>";
		}
		else if($last_claim > time()-(60*$claim_delay) && $type != 2)
		{
			echo "<script>window.onload = function () { alert('You can only claim one order every ".$claim_delay." minutes.'); window.location = 'booster.php'; }</script>";
		}
		else
		{
			$result = mysql_query("SELECT * FROM booster_order WHERE order_record = $order AND active <> 0");

			if(mysql_num_rows($result) == 0)
			{
				mysql_query("INSERT INTO booster_order (booster, order_record, current_tier, wins, current_division, timestamp, losses, last_match) VALUES ($user, $order, '".$row['start_tier']."', 0, ".$row['start_division'].", ".time().", 0, ".time().")") or die(mysql_error());

				header("Location: order.php?id=".$order);
			}
			else
			{
				header("Location: booster.php");
			}
		}
	}
	else
	{
		header("Location: booster.php");
	}
?>